package ink.xiaobaibai.controller.onePay;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.xiaobaibai.common.MySecurity;
import ink.xiaobaibai.entity.FaultDevice;
import ink.xiaobaibai.entity.OzoneDevice;
import ink.xiaobaibai.enumOxy.FaultStatus;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IFaultDeviceService;
import ink.xiaobaibai.service.IOzoneDeviceService;
import ink.xiaobaibai.serviceOxy.ISmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 故障接口
 *
 * @author 小白白
 * @since 2021-05-16
 */
@RestController
@CrossOrigin
@RequestMapping("/fault-device")
@Api(tags = "故障接口")
public class FaultDeviceController {

    @Autowired
    private IOzoneDeviceService ozoneDeviceService;

    @Autowired
    private IFaultDeviceService faultDeviceService;

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private ISmsService smsService;

    /**
     * 用户故障提交申请
     *
     * @param faultDevice
     * @return
     */
    @ApiOperation(value = "用户提交故障申请", notes = "机器id(不可为空),多选故障类型(不可为空),用户自述原因(255字符内,可为空),预留电话(可为空)")
    @PostMapping("/commit")
    public ResponseFormat commit(@RequestBody @Validated FaultDevice faultDevice) {
        QueryWrapper<OzoneDevice> q1 = new QueryWrapper<>();
        q1.select("admin_user_id");
        q1.eq("device_code", faultDevice.getDeviceCode());
        OzoneDevice ozoneDevice = this.ozoneDeviceService.getOne(q1);
        if (ozoneDevice == null) {
            return ResponseFormat.failure("无此机器");
        }
        faultDevice.setUserId(MySecurity.getUserId());
        //初始未受理状态
        faultDevice.setStatus(FaultStatus.UNTREATED.getStatus());
        faultDevice.setAdminUserId(ozoneDevice.getAdminUserId());
        faultDevice.setDepId(this.adminUserService.getDepId(ozoneDevice.getAdminUserId()));
        this.faultDeviceService.save(faultDevice);
        this.smsService.faultUser(faultDevice.getDeviceCode(), faultDevice.getFaultType());
        return ResponseFormat.success();
    }

}

